def main():
    budget = int(input())
    prices = list(map(int, input().split()))
    n = len(prices)
    
    dp = [0] * (budget + 1)
    
    for i in range(n):
        for j in range(i+1, budget+1):
            dp[j] = max(dp[j], dp[j - (i+1)] + prices[i])
            
    print(dp[budget])
    
    # def dfs(i, c):
    #     if i < 0:
    #         return 0
        
    #     if i > c:
    #         return dfs(i-1, c)
        
    #     return max(dfs(i-1, c), dfs(i, c - (i+1)) + prices[i])

    # ans = dfs(n - 1, budget)
    # print(ans)

if __name__ == "__main__":
    main()